package it.blog.controller;

import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.ws.wsaddressing.W3CEndpointReference;



import it.blog.stub.*;

public class UpdatePost extends HttpServlet{


	private static final long serialVersionUID = 1L;
	private BlogPortal bp=null;
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		
		bp = new BlogPortalService().getPort(BlogPortal.class);
		int postID = Integer.parseInt(request.getParameter("postID"));
		Post post = bp.getPost(postID);
		request.getSession().setAttribute("post", post);

		response.sendRedirect(response.encodeRedirectURL(request.getContextPath()+"/private/updatePost.jsp"));

	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {

		Post apost=new Post();
		
		HttpSession session =  request.getSession();
		User user = (User) session.getAttribute("user");
		bp = new BlogPortalService().getPort(BlogPortal.class);
		AuthBlogPortalService us= new AuthBlogPortalService();
		W3CEndpointReference ref=(W3CEndpointReference)session.getAttribute("ref");
		AuthBlogPortal userAuthenticate=us.getPort(ref, AuthBlogPortal.class);
		
		int postID = Integer.parseInt(request.getParameter("postID"));
		apost.setPostID(postID);
		apost.setBlogID(Integer.parseInt(request.getParameter("blogID")));
		apost.setTitle(request.getParameter("titlePost"));
		apost.setContent(request.getParameter("contentPost"));
		apost.setImgURL(request.getParameter("imgURL"));
		apost.setCategory(Integer.parseInt(request.getParameter("postCategory")));
		apost.setCounterPostView(Integer.parseInt(request.getParameter("counterPostView")));	
		apost.setWriter(user.getUserID());
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(System.currentTimeMillis());
		try {
			apost.setDate(toXMLGregorianCalendar(c));
		} catch (DatatypeConfigurationException e) {
				session.setAttribute("updatePostFail",true);
				//Rimando alla pagina cancellando la richiesta corrente
				response.sendRedirect(response.encodeRedirectURL(request.getContextPath()+"/private/updatePost.jsp"));
			}
		
		session.removeAttribute("mess");
		if(!userAuthenticate.updatePost(apost))
			session.setAttribute("mess","aggiornamento post non riuscito");

		response.sendRedirect(response.encodeRedirectURL(request.getContextPath()+"/PostView?blogID="+apost.getBlogID()+"&postID="+apost.getPostID()+"&p=1"));

	}
	
	private static XMLGregorianCalendar toXMLGregorianCalendar(Calendar c) throws DatatypeConfigurationException {
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTimeInMillis(c.getTimeInMillis());
		XMLGregorianCalendar xc = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
		return xc;
	}

}